package com.nCoV.EpidemicSurveillance.repository;


import java.util.List;
import java.util.Map;

import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Component;

import com.nCoV.EpidemicSurveillance.domain.Person;

/**
 * @author 罗穆健
 * @date 2020年3月10日
 */
@Component
public interface PersonRepository extends Neo4jRepository<Person, Long>{
	
	
	@Query("Merge (n:Person{name::#{#name},idCard::#{#idCard}}) return n")
	Person createPerson(String name, String idCard);

	@Query("match(n:Person{name::#{#name},idCard::#{#idCard}}) "
			+ "set n.address = :#{#address} "
			+ "set n.age = :#{#age} "
			+ "set n.sex = :#{#sex} "
			+ "set n.jcbNum = :#{#jcbNum} "
			+ "set n.phone = :#{#phone} "
			+ "set n.email = :#{#email} "
			+ "set n.jcNum = :#{#jcNum} "
			+ "set n.grgl = :#{#grgl} "
			+ "set n.zlqk = :#{#zlqk} "
			+ "set n.glqk = :#{#glqk}")
	void setPatientMsg(String name, String idCard, String address, 
			String age, String sex, String jcbNum, String phone, 
			String email, String jcNum, String grgl, String zlqk, String glqk);

	@Query("Merge (n:Person{name::#{#contactorName},idCard::#{#contactorIdCard}})")
	void createContactor(String contactorName, String contactorIdCard);

	@Query("match(pat:Person{idCard::#{#patientIdCard}}) "
			+ "match(con:Person{idCard::#{#contactorIdCard}}) "
			+ "create (pat)-[r:PersonRelation]->(con)  "
			+ "set r.contactTime=:#{#contactTime}"
			+ "set r.contactName=:#{#contactName} "
			+ "set r.duration=:#{#duration} "
			+ "set r.tfqk=:#{#tfqk} "
			+ "set r.kzqk=:#{#kzqk} "
			+ "set r.dfkz=:#{#dfkz} "
			+ "set r.createDay = :#{#createDay} "
			+ "set r.updateDay = :#{#createDay} "
			+ "set r.sc = 'false' "
			+ "set r.sfxyjs = 'true'")
	void createRelation(String patientIdCard, String contactorName, String contactorIdCard, String contactTime, String contactName, String duration, String tfqk, String kzqk, String dfkz, String createDay, String updateDay);

	@Query("Call apoc.cypher.run(:#{#cxPersonCql}, null) yield value return value as value")
	List<Map<String, Object>> queryContactorByRelation(String cxPersonCql);
	
	@Query("match (n:Person{idCard::#{#idCard},name::#{#name}})-[r:PersonRelation]->(m:Person) "
			+ "return m,r "
			+ " skip :#{#skipNum}"  
			+ " limit :#{#limitNum}")
	List<Map<String, Object>> getContactor(String name, String idCard, int skipNum, int limitNum);
	
	@Query("match(n:Person{idCard::#{#patientIdCard}})-[R:PersonRelation{contactName::#{#contactName}}]->(m:Person{contactorIdCard::#{#contactorIdCard}})" + 
			"return R, m")
	Map<String, Object> queryXqContactorByRelation(String patientIdCard,String contactorIdCard,String contactName);
	
	@Query("match (m:Person{idCard::#{#patientIdCard}})"
			+ "-[r:PersonRelation{contactName::#{#contactName}}]"
			+ "->(n:Person{idCard::#{#contactorIdCard}})"
			+ " delete r")
	void deleteRelationByIdCard(String patientIdCard, String contactorIdCard, String contactName);
	
	@Query("Call apoc.cypher.run(:#{#countCqlString}, null) yield value return value as value")
	Map<String, Object> getPersonCount(String countCqlString);

	@Query("match (n:Person{idCard::#{#patientIdCard}})-[r:PersonRelation]->(m:Person{idCard::#{#contactorIdCard}}) "
			+ "set r.contactName=:#{#contactName} "
			+ "set r.contactTime=:#{#contactTime} "
			+ "set m.phone=:#{#contactorPhone} "
			+ "set r.duration=:#{#duration} "
			+ "set r.tfqk=:#{#tfqk} "
			+ "set r.kzqk=:#{#kzqk} "
			+ "set r.dfkz=:#{#dfkz} "
			+ "set r.updateDay = :#{#updateDay} "
			+ "set r.sfxyjs = 'true'")
	void updatePerson(String patientIdCard, String contactorIdCard, String contactName,
			String contactTime, String contactorPhone, String duration, 
			String tfqk, String kzqk, String dfkz, String updateDay);

	@Query("match (n:Person{idCard::#{#idCard},name::#{#name}}) return n")
	List<Map<String, Object>> getUserSelf(String name, String idCard);
	
	@Query("match (n)-[r:PersonRelation]->(m) "
			+ "return Id(n) as AId,"
			+ "n.age as Aage,"
			+ "n.jcNum as AjcNum,"
			+ "n.grgl as Agrgl,"
			+ "n.jcbNum as AjcbNum,"
			+ "Id(r) as RId,"
			+ "r.contactTime as RcontactTime,"
			+ "r.duration as Rduration,"
			+ "r.tfqk as Rtfqk,"
			+ "r.kzqk as Rkzqk,"
			+ "r.dfkz as Rdfkz,"
			+ "r.contactName as RcontactName,"
			+ "r.sfxyjs as sfxyjs,"
			+ "r.RAgrgl as RAgrgl,"
			+ "r.RBgrgl as RBgrgl,"
			+ "r.sc as sc,"
			+ "Id(m) as BId,"
			+ "m.age as Bage,"
			+ "m.jcNum as BjcNum,"
			+ "m.grgl as Bgrgl,"
			+ "m.jcbNum as BjcbNum")
	List<Map<String, Object>> getAllList();
	
	@Query("match (n:Person) return n.grgl as grgl,"
			+ "n.zlqk as zlqk")
	List<Map<String, Object>> getAllListJd();
	
	@Query("match (n)-[r]->(m) where id(n)=:#{#AId} and id(r)=:#{#RId} and id(m)=:#{#BId} "
			+ "set n.grgl=:#{#Agrgl} "
			+ "set m.grgl=:#{#Bgrgl} "
			+ "set r.RAgrgl=:#{#RAgrgl} "
			+ "set r.RBgrgl=:#{#RBgrgl} "
			+ "set r.sfxyjs=:#{#sfxyjs}")
	void updateP(Long AId, Long RId, Long BId, 
			String Agrgl, String Bgrgl, String RAgrgl, String RBgrgl, String sfxyjs);

	@Query("match (n:Person{idCard::#{#idCard}})-[r:PersonRelation]->(m:Person) where r.sc <> 'true' "
			+ "return m.name as name,m.grgl as grgl,m.address as address,r.contactTime as contactTime,r.contactName as contactName")
	List<Map<String, Object>> getZdjc(String idCard);

	@Query("match (m:Person)-[r:PersonRelation]->(n:Person{idCard::#{#idCard}}) where r.sc <> 'true' "
			+ "return m.name as name,m.grgl as grgl,m.address as address,r.contactTime as contactTime,r.contactName as contactName")
	List<Map<String, Object>> getBdjc(String idCard);

	@Query("match (n:Person) where id(n) = :#{#person.id} "
			+ "set n.age = :#{#person.age} "
			+ "set n.sex = :#{#person.sex} "
			+ "set n.email = :#{#person.email} "
			+ "set n.phone = :#{#person.phone} "
			+ "set n.jcbNum = :#{#person.jcbNum} "
			+ "set n.jcNum = :#{#person.jcNum} "
			+ "set n.address = :#{#person.address} ")
	void updateUserMsg(Person person);

	@Query("match (m:Person{idCard::#{#patientIdCard}})"
			+ "-[r:PersonRelation{contactName::#{#contactName}}]"
			+ "->(n:Person{idCard::#{#contactorIdCard}})"
			+ " set r.sc = 'true' "
			+ " set r.sfxyjs = 'true'")
	void deleteRelationByIdCardT(String patientIdCard, String contactorIdCard, String contactName);

	@Query("match ()-[r:PersonRelation]->() where r.sc = 'true' return Id(r) as id")
	List<Map<String, Object>> getAllListSc();

	@Query("match ()-[r:PersonRelation]->() where id(r) = :#{#id} delete r")
	void deleteRelationSc(Long id);

	@Query("match (n:Person)-[r:PersonRelation]->(m) where n.grgl <> '1.0'  "
			+ "and n.grgl <> '' and n.grgl <> '' and n.grgl <> '0' and n.grgl <> '0.0' "
			+ "return id(n) as id, r.contactTime as contactTime")
	List<Map<String, Object>> getAllPersonNoQz1();
	
	@Query("match (m:Person)-[r:PersonRelation]->(n:Person) where n.grgl <> '1.0'  "
			+ "and n.grgl <> '' and n.grgl <> '' and n.grgl <> '0' and n.grgl <> '0.0' "
			+ "return id(n) as id, r.contactTime as contactTime")
	List<Map<String, Object>> getAllPersonNoQz2();
	
	@Query("match (n:Person) where id(n) = :#{#id} set n.grgl = '0'")
	void updatePerson(Long id);
	
	@Query("match (n:Person) return n.grgl as grgl,"
			+ "n.zlqk as zlqk, n.address as address")
	List<Map<String, Object>> getAllListAddress();

	@Query("match (n:Person)-[r:PersonRelation]->(m:Person) "
			+ "return Id(n) as id,"
			+ "n.age as age,"
			+ "n.jcNum as jcNum,"
			+ "n.grgl as grgl,"
			+ "n.jcbNum as jcbNum,"
			+ "n.address as address,"
			+ "n.sex as sex,"
			+ "n.glqk as glqk,"
			+ "n.phone as phone,"
			+ "n.email as email,"
			+ "n.name as name,"
			+ "n.idCard as idCard")
	List<Map<String, Object>> getDrjcNode1();
	
	@Query("match (m:Person)-[r:PersonRelation]->(n:Person) "
			+ "return Id(n) as id,"
			+ "n.age as age,"
			+ "n.jcNum as jcNum,"
			+ "n.grgl as grgl,"
			+ "n.jcbNum as jcbNum,"
			+ "n.address as address,"
			+ "n.sex as sex,"
			+ "n.glqk as glqk,"
			+ "n.phone as phone,"
			+ "n.email as email,"
			+ "n.name as name,"
			+ "n.idCard as idCard")
	List<Map<String, Object>> getDrjcNode2();
	
	@Query("match (n:Person) "
			+ "return Id(n) as id,"
			+ "n.age as age,"
			+ "n.jcNum as jcNum,"
			+ "n.grgl as grgl,"
			+ "n.jcbNum as jcbNum,"
			+ "n.address as address,"
			+ "n.sex as sex,"
			+ "n.glqk as glqk,"
			+ "n.phone as phone,"
			+ "n.email as email,"
			+ "n.name as name,"
			+ "n.idCard as idCard")
	List<Map<String, Object>> getDrjcNode3();

	@Query("match (n) where id(n) = :#{#id} delete n")
	void deleteNode(Long id);
}
